package com.basic_test;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.Set;

public class selenium_basic_grammar {
    ChromeDriver _chrome_driver;

    public void start_auto_test() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        _chrome_driver = new ChromeDriver(options);
        _chrome_driver.get("https://www.baidu.com/");
    }

    public void quit_auto_test() throws InterruptedException {
        _chrome_driver.quit();
    }

    public void change_window() {
//        String front_title_ = _chrome_driver.getTitle();
//        String front_url = _chrome_driver.getCurrentUrl();
//        System.out.println("title : " + front_title_ + "   url : " + front_url);
//        _chrome_driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
////        为什么我点击了新的链接,然后有了一个新的标签页,但是我获取到的title和url还是之前标签页的title和url?
////        因为新的标签页是我们人可以理解的,但是程序它是不能理解,他并不知道有一个新的标签页,他还以为一直都是原来的标签页
////        所以我们需要让程序知道,它所在的标签页是哪一个
//        String news_title = _chrome_driver.getTitle();
//        String news_url = _chrome_driver.getCurrentUrl();
//        System.out.println("title : " + news_title + "   url : " + news_url);

        String front_title_ = _chrome_driver.getTitle();
        String front_url = _chrome_driver.getCurrentUrl();
//        每一个标签页都会有一个句柄来作为唯一的标识,所以我们只要找到新的标签的句柄
//        那么我们就可以进行标签的跳转,那么程序就知道我当前的标签页进行了跳转
        String front_handle = _chrome_driver.getWindowHandle();
        System.out.println("title : " + front_title_ + "   url : " + front_url);
        _chrome_driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();

//        获取所有页面的句柄
        Set<String> all_handle = _chrome_driver.getWindowHandles();
//        找到新闻页面的句柄
        for (String cur_handle : all_handle) {
            if (!cur_handle.equals(front_handle)) {
//                根据句柄,跳转到这个标签页
                _chrome_driver.switchTo().window(cur_handle);
            }
        }
        String news_title = _chrome_driver.getTitle();
        String news_url = _chrome_driver.getCurrentUrl();
        System.out.println("title : " + news_title + "   url : " + news_url);

    }

    public void window_test() throws InterruptedException {
//        上面的change_window其实有一部分的window的测试语法了(window的选择)

        Thread.sleep(2000);
        _chrome_driver.manage().window().maximize();
        Thread.sleep(2000);
        _chrome_driver.manage().window().minimize();
        Thread.sleep(2000);
        _chrome_driver.manage().window().setSize(new Dimension(1000, 1000));
        Thread.sleep(2000);

        _chrome_driver.get("https://store.steampowered.com/");
        Thread.sleep(2000);

//        selenium提供了执行js的方法
//        滚动页面到底部
        _chrome_driver.executeScript("window.scroll(0, document.body.scrollHeight)");
        Thread.sleep(2000);
//        滚动页面到顶部
        _chrome_driver.executeScript("window.scroll(0, document.body.scrollTop)");
        Thread.sleep(2000);
    }

    public void navigate_test() throws InterruptedException {
        Thread.sleep(1000);
        _chrome_driver.findElement(By.cssSelector("#kw")).sendKeys("我的世界" + Keys.ENTER);
        Thread.sleep(2000);

        //导航页面的前进
        //但是注意这个导航页面的前进和后退只能针对于同一个标签页,不同标签之间,是需要自己进行标签页的切换的,然后才能执行导航页面的前进和后退
        _chrome_driver.navigate().back();
        Thread.sleep(2000);

        //导航页面的后退
        _chrome_driver.navigate().forward();
        Thread.sleep(2000);

    }

    public void alert_test() throws InterruptedException, IOException {
//        _chrome_driver.get("file:///G:/code/java/selenium/selenium4html/modal.html");
//        Thread.sleep(2000);
////        普通弹窗的操作思路
//        _chrome_driver.findElement(By.cssSelector("#show_modal")).click();
//        Thread.sleep(2000);
////        关闭弹窗
//        _chrome_driver.findElement(By.cssSelector("#myModal > div.modal-footer > button:nth-child(1)")).click();
//        Thread.sleep(2000);

//        警告弹窗的操作思路
        _chrome_driver.get("file:///G:/code/java/selenium/selenium4html/confirm.html");
        Thread.sleep(2000);
        _chrome_driver.findElement(By.cssSelector("body > input[type=button]")).click();
        Thread.sleep(2000);
//        切换到警告弹窗上
        Alert alert = _chrome_driver.switchTo().alert();
        Thread.sleep(2000);
//        点击确认按钮
        alert.accept();
        Thread.sleep(2000);
//        页面刷新
        _chrome_driver.navigate().refresh();
        Thread.sleep(2000);
        _chrome_driver.findElement(By.cssSelector("body > input[type=button]")).click();
        Thread.sleep(2000);
        alert = _chrome_driver.switchTo().alert();
        Thread.sleep(2000);
//        点击取消按钮
        alert.dismiss();
        Thread.sleep(2000);
        _chrome_driver.get("file:///G:/code/java/selenium/selenium4html/Prompt.html");
        Thread.sleep(2000);
        _chrome_driver.findElement(By.cssSelector("body > input[type=button]")).click();
        Thread.sleep(2000);
        alert = _chrome_driver.switchTo().alert();
        Thread.sleep(2000);
//        警告弹窗输入文本
        alert.sendKeys("world!");
        Thread.sleep(2000);
        alert.accept();
//        屏幕截图
        File srcfile = _chrome_driver.getScreenshotAs(OutputType.FILE);
//        保存截图的路径
        File filename = new File("./test.png");
        FileUtils.copyFile(srcfile, filename);
        Thread.sleep(2000);
    }

    public void mouse_and_key_test() throws InterruptedException {
//        实际上可以通过查找元素的方式来进行点击链接的效果,但是没有鼠标悬停的效果,就直接进行链接的跳转了
//        _chrome_driver.get("file:///G:/code/java/selenium/selenium4html/level_locate.html");
//        Thread.sleep(2000);
//        _chrome_driver.findElement(By.cssSelector("body > div:nth-child(2) > div > div > a")).click();
//        Thread.sleep(2000);
//        _chrome_driver.findElement(By.cssSelector("#dropdown1 > li:nth-child(2) > a")).click();
//        Thread.sleep(2000);

        _chrome_driver.get("file:///G:/code/java/selenium/selenium4html/level_locate.html");
        Thread.sleep(2000);
        _chrome_driver.findElement(By.cssSelector("body > div:nth-child(2) > div > div > a")).click();
        Thread.sleep(2000);
//        实现鼠标悬停的效果,让鼠标悬停在某一个选项,然后再进行点击链接
        WebElement web_element = _chrome_driver.findElement(By.cssSelector("#dropdown1 > li:nth-child(2) > a"));
        Actions actions = new Actions(_chrome_driver);
//        鼠标悬停
        actions.clickAndHold(web_element).perform();
        Thread.sleep(2000);
//        鼠标点击
        actions.click(web_element).perform();
        Thread.sleep(2000);
    }

    public void select_test() throws InterruptedException {
        _chrome_driver.get("file:///G:/code/java/selenium/selenium4html/select.html");
        Thread.sleep(2000);
        WebElement web_element = _chrome_driver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(web_element);
//        根据select的下标来进行选择
        select.selectByIndex(0);
        Thread.sleep(2000);
//        根据select的值来进行选择
        select.selectByValue("11.61");
        Thread.sleep(2000);
//        根据select的可见文本值来进行选择
        select.selectByVisibleText("UPS 3 Day Select ==> $10.69");
        Thread.sleep(2000);
    }

    public void file_upload_test() throws InterruptedException {
//        可以找到文件选择框,然后直接发送文件的本地地址,就可以达到文件上传的效果
        _chrome_driver.get("file:///G:/code/java/selenium/selenium4html/upload.html");
        _chrome_driver.findElement(By.cssSelector("body > div > div > input[type=file]")).sendKeys("C:\\Users\\qingdiaoshen\\Desktop\\FBDABDD55790D85260597F7BCE6EC769.png");
        Thread.sleep(5000);
    }
}
